/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import com.google.inject.Inject;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import org.jfree.chart.JFreeChart;
import org.xptracker.domain.EffortEntry;
import org.xptracker.domain.Story;
import org.xptracker.domain.TimeSegment;
import org.xptracker.domain.Timeline;
import org.xptracker.services.reports.ReportFactory;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TimeSegmentActionBean extends StandardActionBean {
  @Inject
  private ReportFactory reportFactory;

  private Timeline timeline;
  private TimeSegment segment;
  private String chartUuid;
  private Set<Story> existing;
  private Map<Story, Date> added;
  private Map<Story, Date> removed;
  private Set<Story> removedKeys;
  private Set<Story> completed;
  private Map<String, List<EffortEntry>> storyEffortEntries;

  public Timeline getTimeline() {
    return timeline;
  }

  public TimeSegment getSegment() {
    return segment;
  }

  public Set<Story> getExisting() {
    return existing;
  }

  public Map<Story, Date> getAdded() {
    return added;
  }

  public Map<Story, Date> getRemoved() {
    return removed;
  }

  public Set<Story> getRemovedKeys() {
    return removedKeys;
  }

  public Set<Story> getCompleted() {
    return completed;
  }

  public String getChartUuid() {
    return chartUuid;
  }

  public Map<String, List<EffortEntry>> getStoryEffortEntries() {
    return storyEffortEntries;
  }

  public void setStoryEffortEntries(Map<String, List<EffortEntry>> storyEffortEntries) {
    this.storyEffortEntries = storyEffortEntries;
  }

  @DefaultHandler
  public Resolution displaySegment() {
    timeline = loadTimeline();
    segment = loadSegment();

    final HttpSession session = getContext().getRequest().getSession();

    WebReportRecipient recipient = new WebReportRecipient(segment);
    reportFactory.makeTimeSegmentReport(timeline, segment).execute(recipient);

    JFreeChart chart = recipient.buildChart();
    chartUuid = ChartActionBean.storeChart(new SessionContext() {
      public void setAttribute(String key, Object value) {
        session.setAttribute(key, value);
      }
    }, chart);

    existing = recipient.getExisting();
    removed = recipient.getRemoved();
    added = recipient.getAdded();
    completed = recipient.getCompleted();
    removedKeys = removed.keySet();
    storyEffortEntries = recipient.getEffortEntries(domain);
    return new ForwardResolution("/timeSegment.jsp");
  }

  private TimeSegment loadSegment() {
    return domain.loadEntity(TimeSegment.class, param("segmentId"));
  }

  private Timeline loadTimeline() {
    return domain.loadEntity(Timeline.class, param("timelineId"));
  }
}
